import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:get/get.dart';
import 'package:sports_new/controller/expert/plan_pack/plan_pack_logic.dart';
import 'package:sports_new/entity/expert/plan_pack/plan_pack_entity.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/resources/styles.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/view/pick/items/avator_label.dart';
import 'package:sports_new/widgets/base/qkd_text.dart';
import 'package:sports_new/widgets/card.dart';
import 'package:sports_new/widgets/circular.dart';
import 'package:sports_new/widgets/loading/loading_widget.dart';
import '../../../utils/utils.dart';
import '../../home/qkd_carousel_view.dart';

class PlanPackPage extends StatefulWidget {
  const PlanPackPage({super.key});

  @override
  State<PlanPackPage> createState() => _PlanPackPageState();
}

class _PlanPackPageState extends State<PlanPackPage> {
  final PlanPackLogic logic = Get.put(PlanPackLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<PlanPackLogic>(builder: (logic) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        backgroundColor: Colours.greyF2F3F7,
        appBar: Styles.appBar(
            title: const QkdText("专家方案套餐"),
            backgroundColor: Colours.transparent,
            actions: [
              TextButton(
                      onPressed: () {
                        Get.toNamed(Routes.planPackHistory);
                      },
                      child: const QkdText('历史套餐',
                          color: Colours.text,
                          fontSize: 14,
                          fontWeight: FontWeight.w400))
                  .paddingOnly(right: 8)
            ]),
        body: Column(
          children: [
            Image.asset(
              Utils.getImgPath("nav_head_back.png"),
              width: Get.width,
              height: Get.mediaQuery.padding.top + 40,
              fit: BoxFit.cover,
            ),
            Expanded(
              child: EasyRefresh.builder(
                onRefresh: logic.getData,
                childBuilder: (BuildContext context, ScrollPhysics physics) =>
                    Container(
                  decoration: const BoxDecoration(
                      gradient: LinearGradient(
                          colors: [Colours.white, Colours.greyF2F3F7],
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          stops: [0, 0.1])),
                  child: CustomScrollView(physics: physics, slivers: [
                    if (logic.lbt.hasValue)
                      SliverToBoxAdapter(
                        child: QkdCarouselView(
                          key: Key("${DateTime.now()}"),
                          aspectRatio: 359 / 120,
                          radius: 8,
                          padding: 8,
                          needDot: (logic.lbt ?? []).length > 1,
                          carousels: logic.lbt ?? [],
                          tapType: "fatc_lbt",
                          needTitle: false,
                        ).paddingOnly(top: 8),
                      ),
                    LoadingWidget<bool>.sliver(
                      isLoading: logic.data == null,
                      data: logic.data?.length == 0,
                      child: () => SliverList.builder(
                          itemCount: logic.data?.length,
                          itemBuilder: (context, index) =>
                              item(logic.data![index])),
                    )
                  ]),
                ),
              ),
            ),
          ],
        ),
      );
    });
  }

  Widget item(PlanPackEntity entity) {
    return Block(
        onTap: () =>
            Get.toNamed(Routes.planPackDetail, arguments: entity.packageId),
        cardStyle: const CardStyle(
            padding: EdgeInsets.all(12),
            margin: EdgeInsets.fromLTRB(8, 8, 8, 0)),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            AvatarLabel.record(
                avatarUrl: entity.expertLogo,
                name: entity.expertName,
                id: entity.expertId,
                firstTag: entity.tag),
            QkdText(entity.title ?? "", maxLines: 99)
                .paddingSymmetric(vertical: 8),
            Circular(
                color: Colours.redFFF7F7,
                radius: 15,
                child: DefaultTextStyle(
                  style: const TextStyle(fontSize: 12, color: Colours.grey99),
                  child: Row(
                    children: [
                      Container(width: 12),
                      const QkdText("套餐价："),
                      QkdText("${entity.price ?? ""}球币".changeUnit,
                          fontSize: 14, color: Colours.main),
                      if (entity.oldPrice.hasValue)
                        QkdText("原价${entity.oldPrice ?? ""}球币".changeUnit,
                                decoration: TextDecoration.lineThrough)
                            .paddingOnly(left: 4),
                      const Spacer(),
                      GestureDetector(
                        child: Circular(
                            width: 74,
                            height: 30,
                            radius: 18,
                            alignment: Alignment.center,
                            gradient: const LinearGradient(
                              colors: [
                                Color(0xFFFF649C),
                                Color(0xFFFF4667),
                                Color(0xFFFF5454),
                                Color(0xFFFF8064)
                              ],
                            ),
                            child: QkdText(
                              entity.isBuy == 1 ? "立即查看" : "立即购买",
                              fontSize: 13,
                              fontWeight: FontWeight.w500,
                              color: Colours.white,
                              needCenter: true,
                            )),
                      )
                    ],
                  ),
                ))
          ],
        ));
  }
}
